package perfumecabinet

import (
	"context"

	"mall/common/globalkey"
	"mall/common/resultx"
	"mall/service/forum/model"
	"mall/service/forum/rpc/internal/svc"
	"mall/service/forum/rpc/types/forum"

	"github.com/zeromicro/go-zero/core/logx"
)

type DeletePerfumeCabinetLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewDeletePerfumeCabinetLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeletePerfumeCabinetLogic {
	return &DeletePerfumeCabinetLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *DeletePerfumeCabinetLogic) DeletePerfumeCabinet(in *forum.DeletePerfumeCabinetReq) (*forum.BaseResp, error) {
	if in.UserId == nil || *in.UserId == 0 {
		return nil, resultx.StatusError(resultx.FORUM_PERFUME_CABINET_USER_ID_REQUIRED, "")
	}
	if len(in.Ids) == 0 {
		return nil, resultx.StatusError(resultx.FORUM_PERFUME_CABINET_ID_REQUIRED, "")
	}
	var ids []any
	for _, id := range in.Ids {
		ids = append(ids, id)
	}
	var filterMap = make(map[string]map[string][]any)
	filterMap[model.ForumUserPerfumeCabinetColumns.Status] = map[string][]any{
		"notIn": []any{globalkey.StatusDeleted},
	}
	filterMap[model.ForumUserPerfumeCabinetColumns.ID] = map[string][]any{
		"in": ids,
	}
	filterMap[model.ForumUserPerfumeCabinetColumns.UserID] = map[string][]any{
		"equalTo": []any{*in.UserId},
	}
	perfumeList, _, err := l.svcCtx.ForumPerfumeCabinet.GetList(l.ctx, model.M{}, -1, filterMap)
	if err != nil {
		return nil, err
	}
	if len(perfumeList) == 0 {
		return &forum.BaseResp{}, nil
	}
	for _, v := range perfumeList {
		if *in.UserId != v.UserID {
			continue
		}
		v.Status = uint8(globalkey.StatusDeleted)
		err = l.svcCtx.ForumPerfumeCabinet.Update(l.ctx, nil, v)
		if err != nil {
			return nil, err
		}
	}
	return &forum.BaseResp{
		Msg: "删除成功",
	}, nil
}
